package com.tan.androidtest.activity;

import android.annotation.TargetApi;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;

import com.tan.androidtest.R;
import com.tan.androidtest.base.BaseActivity;

import java.io.File;
import java.io.IOException;

/**
 * Created by txj on 2018/2/25.
 * 测试背景音频的开启关闭
 * 今日头条中有这样的功能:
 * 视频开始播放的时候关闭音乐
 */

public class TestAudioMuteActivity extends BaseActivity implements View.OnClickListener {

    private MediaPlayer mediaPlayer = new MediaPlayer();
    private MediaPlayer mediaPlayer1 = new MediaPlayer();
    private AudioManager mAudioManager;
    private MyAudioFocusChangeListener mFocusChangeListener;

    @Override
    public int initLayoutId() {
        return R.layout.activity_test_audio_mute_layout;
    }

    @Override
    public void initTitle(TextView tv_bar_left, TextView tv_bar_title, TextView tv_bar_right) {
        tv_bar_title.setText("测试背景音乐的开启关闭");
    }

    @Override
    public void initView() {
        TextView tv_test = findViewById(R.id.tv_test);
        tv_test.setOnClickListener(this);
        TextView tv_test_1 = findViewById(R.id.tv_test_1);
        tv_test_1.setOnClickListener(this);
        TextView tv_stop = findViewById(R.id.tv_stop);
        tv_stop.setOnClickListener(this);
        TextView tv_restart = findViewById(R.id.tv_restart);
        tv_restart.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        File file = new File(Environment.getExternalStorageDirectory(), "timian.mp3");
        try {
            mediaPlayer.setDataSource(file.getPath());
            mediaPlayer.prepare();
            mediaPlayer1.setDataSource(file.getPath());
            mediaPlayer1.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_test: {
                //mediaPlayer.start();
                startMediaWithAudioFocus();
                break;
            }
            case R.id.tv_test_1: {
                //mediaPlayer1.start();
                startMediaWithAudioFocus();
                break;
            }
            case R.id.tv_stop: {
                muteAudioFocus(this, true);
                break;
            }
            case R.id.tv_restart: {
                muteAudioFocus(this, false);
                break;
            }
        }
    }

    public void startMediaWithAudioFocus() {
        int result = mAudioManager.requestAudioFocus(mFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            mediaPlayer.start();
        }
    }

    public void startMediaWithAudioFocus1() {
        int result = mAudioManager.requestAudioFocus(mFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            mediaPlayer1.start();
        }
    }

    private void startPlayMp3() {
        File file = new File(Environment.getExternalStorageDirectory(), "timian.mp3");
        MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(file.getPath());
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param bMute 值为true时为关闭背景音乐。
     */
    @TargetApi(Build.VERSION_CODES.FROYO)
    public boolean muteAudioFocus(Context context, boolean bMute) {
        boolean bool = false;
        AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (bMute) {
            int result = am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
            bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
        } else {
            int result = am.abandonAudioFocus(null);
            bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
        }
        return bool;
    }

    class MyAudioFocusChangeListener implements AudioManager.OnAudioFocusChangeListener {

        private int mOriginalVol;

        private int mPreviousState;

        private boolean mShouldStart = true;

        @Override
        public void onAudioFocusChange(int focusChange) {
            handlerAudioFocus(focusChange);
            mPreviousState = focusChange;
        }

        private void handlerAudioFocus(int focusChange) {
            if (focusChange != AudioManager.AUDIOFOCUS_GAIN) {
                mOriginalVol = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            }
            switch (focusChange) {
                case AudioManager.AUDIOFOCUS_GAIN:
                    handlerAudioFocusGain();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS:
                    mediaPlayer.release();
                    mediaPlayer = null;
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    mediaPlayer.pause();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mOriginalVol / 2, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                    break;
            }
        }

        private void handlerAudioFocusGain() {
            switch (mPreviousState) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    //initPlayer();
                    mShouldStart = false;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    if (mShouldStart) {
                        mediaPlayer.start();
                    } else {
                        mShouldStart = true;
                    }
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mOriginalVol, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                    break;
                default:
            }
        }
    }


}
